﻿using System.Collections;
using System.Collections.Generic;
using TrueSync;
using Framework;

public enum SkillTypeEnum
{
    SuperKillSkill = 0,
    KillSkill,
    SpecialSkill,
    NormalSkill,
}

[System.Serializable]
public class SkillGroup
{
    //技能组名字
    public string Name;
    public string TriggerInput;
    //技能组ID
    public int SkillGroupID;
    //技能流数组
    public List<int> SkillStreamIDArray;
    //子物体数组
    public List<int> SubItemIDArray;
    //技能组描述
    public string Description;
    //技能类型,可多选
    public List<bool> SkillType;
    //是否检测状态机
    public bool DetectCurrentStateType;
    public List<StateEnum> RequiredState;
    //是否检测边界距离
    public bool DetectBoundaryDistance;
    public enum DetectBoundaryTypeEnum
    {
        ForwardBoundary,
        BackwardBoundary,
    }
    public DetectBoundaryTypeEnum DetectBoundaryType;
    public FP DetectDistance;
    public FP DetectHeight;
    //部件需求
    public bool DetectWeapon;
    public enum RequiredWeaponEnum
    {
        Weapon1,
        Weapon2,
        HiddenWeapon1,
        HiddenWeapon2
    }
    public RequiredWeaponEnum RequiredWeapon;
    //状态检测
    public bool DetectStatus;
    public List<int> AllowWhenHaveStatus;
    public List<int> DisAllowWhenHaveStatus;
    //变形检测
    public bool DetectModelID;
    public int AllowModelID;
    //消耗怒气
    public bool DetectEnergyConsumption;
    public int EnergyConsumption;
    //高度检测
    public bool DetectPositionY;
    public FP MinHeight;
    //是否继承之前状态机运动轨迹
    public bool UseLastStateMoveSpeed;
    //是否继承之前状态机位置
    public bool UseLastStatePosition;

    //首技能流ID,这个是自动生成的
    public int InitialSkillStreamID;

    //判定能不能执行技能组
    public bool CanUseSkill(ObjHandle<LogicEntity> owner)
    {
        if (owner.IsValid == false)
            return false;

        //当前技能流是不是能够被打断
        LogicStateMachineComponent stateMachineComponent = owner.handle.GetLogicComponent<LogicStateMachineComponent>();
        if (stateMachineComponent.CurrentState.StateName == StateEnum.SkillStream)
        {
            if (stateMachineComponent.AllowSkillBreak == false)
                return false;
            SkillStreamState skillStreamState = (SkillStreamState)stateMachineComponent.CurrentState;
            if (((skillStreamState.AllowSpecialSkillBreak && SkillType[(int)SkillTypeEnum.SuperKillSkill]) ||
                (skillStreamState.AllowKillSkillBreak && SkillType[(int)SkillTypeEnum.KillSkill]) ||
                (skillStreamState.AllowSpecialSkillBreak && SkillType[(int)SkillTypeEnum.SpecialSkill]) ||
                skillStreamState.AllowSkillBreakIDs.Contains(SkillGroupID)) == false)
                return false;

        }
        //检查状态机状态
        if (DetectCurrentStateType)
        {
            if (RequiredState.Contains(stateMachineComponent.CurrentState.StateName) == false)
                return false;
        }
        //检查边界
        if (DetectBoundaryDistance)
        {

        }
        //检查部件
        if (DetectWeapon)
        {

        }
        //检查状态
        if (DetectStatus)
        {

        }
        //检查变形
        if (DetectModelID)
        {

        }
        //检查高度
        if(DetectPositionY)
        {
            if (owner.handle.GetLogicComponent<LogicMoveComponent>().Position.y < MinHeight)
                return false;
        }

        //检查能量
        if(DetectEnergyConsumption)
        {
            LogicStatusComponent ownerStatus = owner.handle.GetLogicComponent<LogicStatusComponent>();
            if (ownerStatus.Energy < EnergyConsumption)
                return false;
        }
        return true;
    }

    public void Serialize(EByte ms)
    {
        ms.WriteString(Name);
        ms.WriteString(TriggerInput);
        ms.WriteInt(SkillGroupID);

        ms.WriteInt(SkillStreamIDArray.Count);
        for(int i = 0; i < SkillStreamIDArray.Count; i++)
        {
            ms.WriteInt(SkillStreamIDArray[i]);
        }

        ms.WriteInt(SubItemIDArray.Count);
        for(int i = 0; i < SubItemIDArray.Count; i++)
        {
            ms.WriteInt(SubItemIDArray[i]);
        }

        ms.WriteString(Description);

        ms.WriteInt(SkillType.Count);
        for(int i = 0; i < SkillType.Count; i++)
        {
            ms.WriteInt(SkillType[i] ? 1 : 0);
        }

        ms.WriteInt(DetectCurrentStateType ? 1 : 0);
        ms.WriteInt(RequiredState.Count);
        for(int i = 0; i < RequiredState.Count; i++)
        {
            ms.WriteInt((int)RequiredState[i]);
        }

        ms.WriteInt(DetectBoundaryDistance ? 1 : 0);
        ms.WriteInt((int)DetectBoundaryType);
        ms.WriteLong(DetectDistance.RawValue);
        ms.WriteLong(DetectHeight.RawValue);

        ms.WriteInt(DetectWeapon ? 1 : 0);


        ms.WriteInt((int)RequiredWeapon);
    

        ms.WriteInt(DetectStatus ? 1 : 0);
        ms.WriteInt(AllowWhenHaveStatus.Count);
        for(int i = 0; i < AllowWhenHaveStatus.Count; i++)
        {
            ms.WriteInt(AllowWhenHaveStatus[i]);
        }
        ms.WriteInt(DisAllowWhenHaveStatus.Count);
        for (int i = 0; i < DisAllowWhenHaveStatus.Count; i++)
        {
            ms.WriteInt(DisAllowWhenHaveStatus[i]);
        }

        ms.WriteInt(DetectModelID ? 1 : 0);
        ms.WriteInt(AllowModelID);

        ms.WriteInt(DetectEnergyConsumption ? 1 : 0);
        ms.WriteInt(EnergyConsumption);

        ms.WriteInt(DetectPositionY ? 1 : 0);
        ms.WriteLong(MinHeight.RawValue);

        ms.WriteInt(UseLastStateMoveSpeed ? 1 : 0);
        ms.WriteInt(UseLastStatePosition ? 1 : 0);
    }

    public void Deserialize(EByte ms)
    {
        Name = ms.ReadString();
        TriggerInput = ms.ReadString();
        SkillGroupID = ms.ReadInt();

        SkillStreamIDArray = new List<int>(ms.ReadInt());
        for(int i = 0; i < SkillStreamIDArray.Count; i++)
        {
            SkillStreamIDArray[i] = ms.ReadInt();
        }

        SubItemIDArray = new List<int>(ms.ReadInt());
        for(int i = 0; i < SubItemIDArray.Count; i++)
        {
            SubItemIDArray[i] = ms.ReadInt();
        }

        Description = ms.ReadString();

        SkillType = new List<bool>(ms.ReadInt());
        for(int i = 0; i < SkillType.Count; i++)
        {
            SkillType[i] = ms.ReadInt() == 1;
        }

        DetectCurrentStateType = ms.ReadInt() == 1;
        RequiredState = new List<StateEnum>(ms.ReadInt());
        for(int i = 0; i < RequiredState.Count; i++)
        {
            RequiredState[i] = (StateEnum)ms.ReadInt();
        }

        DetectBoundaryDistance = ms.ReadInt() == 1;
        DetectBoundaryType = (DetectBoundaryTypeEnum)ms.ReadInt();
        DetectDistance = FP.FromRaw(ms.ReadLong());
        DetectHeight = FP.FromRaw(ms.ReadLong());

        DetectWeapon = ms.ReadInt() == 1;
        RequiredWeapon = (RequiredWeaponEnum)ms.ReadInt();
     

        DetectStatus = ms.ReadInt() == 1;
        AllowWhenHaveStatus = new List<int>(ms.ReadInt());
        for(int i = 0; i < AllowWhenHaveStatus.Count; i++)
        {
            AllowWhenHaveStatus[i] = ms.ReadInt();
        }
        DisAllowWhenHaveStatus = new List<int>(ms.ReadInt());
        for(int i = 0; i < DisAllowWhenHaveStatus.Count; i++)
        {
            DisAllowWhenHaveStatus[i] = ms.ReadInt();
        }

        DetectModelID = ms.ReadInt() == 1;
        AllowModelID = ms.ReadInt();

        DetectEnergyConsumption = ms.ReadInt() == 1;
        EnergyConsumption = ms.ReadInt();

        DetectPositionY = ms.ReadInt() == 1;
        MinHeight = FP.FromRaw(ms.ReadLong());

        UseLastStateMoveSpeed = ms.ReadInt() == 1;
        UseLastStatePosition = ms.ReadInt() == 1;
    }
}
